﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace dzs.Web.SysManage
{
    public partial class ListRoles : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Bind();
            }
        }

        protected void gv_data_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.gv_data.PageIndex = e.NewPageIndex;

            Bind();

        }
        protected void gv_data_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#dbf496'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor");
            }

        }
        protected void gv_data_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "up")
            {
                if (e.CommandArgument != null)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>location.href='AddRoles.aspx?roles_id=" + e.CommandArgument + "'</script>");
                }
            }
            if (e.CommandName == "del")
            {
                if (e.CommandArgument != null)
                {
                    if (e.CommandArgument.ToString() == "1")//系统管理员
                    {
                        ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('系统管理员无法删除！')</script>");
                    }
                    else
                    {
                        dzs.BLL.roles rm = new BLL.roles();
                        dzs.BLL.role_right rrm = new BLL.role_right();

                        int roles_id = int.Parse(e.CommandArgument.ToString());

                        rrm.DeleteByRolesId(roles_id);
                        rm.Delete(roles_id);

                    }
                    Bind();
                }
            }
            if (e.CommandName == "dist")//分配权限
            {
                if (e.CommandArgument != null)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>location.href='DistributeRoles.aspx?roles_id=" + e.CommandArgument + "'</script>");
                }
            }
        }

        private void Bind()
        {
            DataSet ds = new dzs.BLL.roles().GetAllList();

            this.gv_data.DataSource = ds;
            this.gv_data.DataBind();
        }
    }
}